package  com.earstep.view
{
	import com.erastep.mvc.EraStepFrame;
	import com.erastep.mvc.ifc.IFrame;
	import com.erastep.mvc.ifc.INotification;
	import com.erastep.mvc.mediator.Mediator;
	import com.erastep.mvc.module.vo.ModuleVO;
	import com.erastep.util.CommonConstants;
	import com.erastep.util.GUID;
	import com.erastep.util.LogUtil;
	import com.erastep.util.StringUtil;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.getDefinitionByName;
	
	import mx.controls.Alert;
	
	/**
	 * @depart 宁波维涛
	 * @author Jarvis 
	 * @E-mail: shaomj@victopsoft.com
	 * @version 1.0.1 
	 * 创建时间：2011-11-29 上午09:58:26 * 
	 */
	public class CommonNotificationsMediator extends Mediator
	{
		
		public static const NAME:String = "commonNotificationsMediator";
		
		private var popObject:Object;
		
		private var loader:Loader;
		
		private var context:LoaderContext;
		
		public function CommonNotificationsMediator(view:Object)
		{
			super(NAME, view);
			popObject = new Object();
		}
		
		/**
		 * 链接到其他表单
		 * */
		public function respondToLoadLinkModule(note:INotification):void{
			var param:Object = note.getBody();
			LogUtil.print("开始查找模版!");
			//开始加载插件
			var iframe:IFrame = EraStepFrame.getInstance();
			var tempvo:ModuleVO = iframe.findTemplateVoBySF(int(param.systemId),int(param.formId));
			if(tempvo != null){
				var key:String;
				if(param.hasOwnProperty("key")){
					key = param.key;
				}else{
					key = GUID.create();
				}
				if(!iframe.hasLoadedModule(tempvo.resourceid)){
					loader=new Loader();
					context=new LoaderContext(false, ApplicationDomain.currentDomain);
					loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void{
						LogUtil.print("ID:" + tempvo.resourceid + "   swfUrl:" + tempvo.url + " Load complete!");
						var cls:Class = getDefinitionByName("com.victop."+tempvo.resourceid+".Main") as Class;
						// at here register module
						iframe.registerModule(tempvo.resourceid, cls);
						LogUtil.print("$$%%$$"+StringUtil.objectToJson(param));
						sendNotification(tempvo.resourceid,param,key,CommonConstants.MODULE);		
					});
					loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void{
						LogUtil.print("########Load Fail:"+StringUtil.objectToJson(tempvo));
					});
					
					LogUtil.print("插件加载路径:"+host+"/"+tempvo.url);
					loader.load(new URLRequest(host+"/"+tempvo.url),context);
					//监听进度条					
					loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, handleProgress); 
				}else{
					sendNotification(tempvo.resourceid,param,key,CommonConstants.MODULE);	
				}
			}else{
				Alert.show("请确认systemId:"+int(param.systemId)+"和 formId:"+int(param.formId)+" 的值!");
				LogUtil.print("未找到对应的模版..");
			}
		}
		/**
		 * 加载插件 进度条
		 * */
		private function handleProgress(e:ProgressEvent):void{
			trace("插件=========已加载:"+e.bytesLoaded+" 文件大小:"+e.bytesTotal);
		}
	}
}